package org.itboys.seller;

import org.itboys.admin.constant.AdminSessionConstant;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.servlet.ServletContextHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.shop.entity.ShopSeller;
import org.itboys.shop.service.ShopSellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 商家
 * @author weisky
 *
 */
@RestController
@RequestMapping("/seller/edit")
public class SellerManageController extends BaseController{

    @Autowired
    private ShopSellerService shopSellerService;

    /**
     * 跳到商家管理页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/toListVM")
    public ModelAndView toListVM(Model model,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {
        long shopId = (Long) ServletContextHolder.getSession().getAttribute(AdminSessionConstant.SHOP_ID);
        model.addAttribute("seller", shopSellerService.getById(shopId));
        return new ModelAndView("/seller/seller-base");
    }

    /**
     * 新增或者修改
     * @param seller
     * @param logos:商家logo
     * @param mainImgs：主图
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public void save(ShopSeller seller,
                     @RequestParam(required=false) MultipartFile logos,
                     @RequestParam(required=false) MultipartFile mainImgs,
                     HttpServletRequest request, HttpServletResponse response){

        if (shopSellerService.insert(seller, logos, mainImgs, null, null, null)) {
            AjaxUtils.renderJson(response, shopSellerService.getById(seller.getId()));
        } else {
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 获取一条数据
     * @param id
     * @param request
     * @param response
     */
    @RequestMapping("/getById")
    public void getById(Long id, HttpServletRequest request, HttpServletResponse response){
        AjaxUtils.renderJson(response, shopSellerService.getById(id));
    }
}
